package com.example.plus.file.copyCharset;

import com.example.plus.file.comm.util.IoUtil;
import com.example.plus.file.copyCharset.action.FileCharsetCopyAction;
import org.junit.Test;

import java.io.*;

/**
 * 文件复制的copy
 * 主要实现从一个文件的编码复制到另一个文件的编码，并且保证不乱码
 *
 * @author zhouxl
 * @DATE 2020/12/10
 * @descriptoin
 */
public class FileCopyCharsetTest {

    /**
     * 复制jar文件
     *
     * @throws IOException
     */
    @Test
    public void copyFileJarTest() throws IOException {
//        D:\springboot-demo\maven-parent\springboot-code-test\.mvn\wrapper\maven-wrapper.jar
        String inPath = "D:\\springboot-demo\\maven-parent\\springboot-code-test\\.mvn\\wrapper\\maven-wrapper.jar";
        String outPath = "D:\\新建文件夹\\test文件\\maven-wrapper.jar";
        String sourceCharset = "utf-8";
        String targetCharset = "utf-8";
        BufferedReader reader = IoUtil.bufferedReader(new InputStreamReader(new FileInputStream(inPath), sourceCharset));
        BufferedWriter writer = IoUtil.bufferedWriter(new OutputStreamWriter(new FileOutputStream(outPath), targetCharset));
        String line = null;
        while ((line = reader.readLine()) != null) {
            writer.write(line);
            writer.newLine();
        }
        writer.flush();
        // 关闭流信息
        IoUtil.closeable(reader, writer);
    }

    /**
     * 测试那些文件不能使用 read.line
     */
    /**
     * 测试文件复制处理
     * 那些文件格式不能使用
     */
    @Test
    public void copyFileMdTest() throws Exception {
        String inPath = "D:\\springboot-example\\springboot-example\\README.en.md";
        String outPath = "D:\\新建文件夹\\test文件\\README.en.md";
        String sourceCharset = "utf-8";
        String targetCharset = "utf-8";
        BufferedReader reader = IoUtil.bufferedReader(new InputStreamReader(new FileInputStream(inPath), sourceCharset));
        BufferedWriter writer = IoUtil.bufferedWriter(new OutputStreamWriter(new FileOutputStream(outPath), targetCharset));
        String line = null;
        while ((line = reader.readLine()) != null) {
            writer.write(line);
            writer.newLine();
        }
        writer.flush();
        // 关闭流信息
        IoUtil.closeable(reader, writer);
    }

    /**
     * 测试
     */
    @Test
    public void copyFile2Test() throws Exception {
        FileCharsetCopyAction action = new FileCharsetCopyAction();
        String inPath = "D:\\新建文件夹\\test文件\\sql文件";
        String outPath = "D:\\新建文件夹\\test文件\\sql2文件_utf8";
        String sourceCharset = "gb2312";
        String targetCharset = "utf-8";
        action.copyFile(inPath, sourceCharset, outPath, targetCharset);

    }

    /**
     * 测试文件复制处理
     */
    @Test
    public void copyFileTest() throws Exception {
        String inPath = "D:\\新建文件夹\\test文件\\sql文件\\ue01_zh.sql";
        String outPath = "D:\\新建文件夹\\test文件\\ue01_zh.sql";
        String sourceCharset = "gb2312";
        String targetCharset = "utf-8";
        BufferedReader reader = IoUtil.bufferedReader(new InputStreamReader(new FileInputStream(inPath), sourceCharset));
        BufferedWriter writer = IoUtil.bufferedWriter(new OutputStreamWriter(new FileOutputStream(outPath), targetCharset));
        String line = null;
        while ((line = reader.readLine()) != null) {
            writer.write(line);
            writer.newLine();
        }
        writer.flush();
        // 关闭流信息
        IoUtil.closeable(reader, writer);
    }

}
